package com.lgmshare.k3.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.lgmshare.k3.receiver.NotifyReceiver;

/**
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2018/3/27 16:57
 */
public class NotifyService extends Service {

    /**
     * 定时唤醒的时间间隔，1分钟
     */
    private final static int ALARM_INTERVAL = 30 * 1000;
    private final static int REQUEST_CODE_WAKE = 9438;

    public NotifyService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //发送唤醒广播来促使挂掉的UI进程重新启动起来
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent();
        alarmIntent.setAction(NotifyReceiver.ACTION_WAKE_NOTIFY);
        PendingIntent operation = PendingIntent.getBroadcast(this, REQUEST_CODE_WAKE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), ALARM_INTERVAL, operation);
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

}
